/**
 * 全局消息提示配置，统一信息入口，方便全局调整
 * @author luguoxiang
 * @date 2022/9/18
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
import { DialogPlugin, MessagePlugin, NotifyPlugin } from 'tdesign-vue-next';

interface BaseOptions {
  /**
   * 信息内容
   */
  message: string;
  /**
   * 图标
   */
  icon?: any;
  [p: string]: any;
}

export interface ConfirmOptions extends BaseOptions {
  /**
   * 是否开启确认框，默认：true，传入false则直接执行确认回调，不会弹窗询问用户
   */
  enabled?: boolean;
  /**
   * 信息标题
   */
  title?: string;
  /**
   * 确认按钮文字
   */
  okText?: string;
  /**
   * 取消按钮文字
   */
  cancelText?: string;
  /**
   * 是否可拖拽，默认：true
   */
  draggable?: boolean;
}

export interface InfoOptions extends BaseOptions {
  /**
   * 延时时间
   */
  duration?: number;
}

export interface SuccessOptions extends InfoOptions {
  /**
   * 信息标题
   */
  title?: string;
}

export interface WarningOptions extends InfoOptions {
  /**
   * 信息标题
   */
  title?: string;
}

export interface ErrorOptions extends InfoOptions {
  /**
   * 信息标题
   */
  title?: string;
}

/**
 * 确认消息
 */
export function confirm(options: ConfirmOptions | string): Promise<any> {
  if (typeof options === 'string') {
    options = { message: options };
  } else {
    options = { ...options };
  }
  options.body = options.message;
  delete options.message;
  options.header = options.title ?? '确认您的操作';
  delete options.title;
  options.confirmBtn = options.okText ?? '确定';
  delete options.okText;
  options.cancelBtn = options.cancelText ?? '取消';
  delete options.cancelText;

  if (options.enabled !== false) {
    return new Promise<any>((resolve, reject) => {
      const confirm = DialogPlugin.confirm({
        destroyOnClose: true,
        // @ts-ignore
        ...options,
        onClosed: reject,
        onConfirm: () => {
          resolve(confirm);
          setTimeout(confirm.hide, 300);
        },
      });
    });
  }
  return Promise.resolve();
}

/**
 * 提示消息
 */
export function info(options: InfoOptions | string): Promise<unknown> {
  if (typeof options === 'string') {
    options = { message: options };
  }
  options.content = options.message;
  delete options.message;
  return MessagePlugin.info(options);
}

/**
 * 成功消息
 */
export function success(options: SuccessOptions | string): Promise<unknown> {
  if (typeof options === 'string') {
    options = { message: options };
  }
  options.title ??= '成功';
  options.content = options.message;
  options.closeBtn ??= true;
  delete options.message;
  return NotifyPlugin.success(options);
}

/**
 * 警告消息
 */
export function warning(options: WarningOptions | string): Promise<unknown> {
  if (typeof options === 'string') {
    options = { message: options };
  }
  options.title ??= '警告';
  options.content = options.message;
  options.closeBtn ??= true;
  delete options.message;
  return NotifyPlugin.warning(options);
}

/**
 * 错误消息
 */
export function error(options: ErrorOptions | string): Promise<unknown> {
  if (typeof options === 'string') {
    options = { message: options };
  }
  options.title ??= '失败';
  options.content = options.message;
  options.closeBtn ??= true;
  delete options.message;
  return NotifyPlugin.error(options);
}

export default {
  confirm,
  info,
  success,
  warning,
  error,
};
